an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text

jupyter stands for julia, python, and r



setup

jupyter notebook is included in the anaconda distribution - see notes on conda

however, here are some options if you need to install it manaully:

python3 -m pip install jupyter
python -m pip install jupyter
pip3 install jupyter
pip install jupyter
conda install jupyter

kernels

The ipython kernel is the python execution backend for jupyter

# list all kernels
jupyter kernelspec list

# add a kernel using an existing conda environment
myenv=how2-py
conda activate $myenv
pip install -U ipykernel
python -m ipykernel install --user --name $myenv --display-name $myenv
jupyter kernelspec list

# delete a kernel
jupyter kernelspec uninstall unwanted-kernel

See which python you are using - i.e. the python associated with the kernel

import sys
print(sys.executable)
print(sys.version)

See which python jupyter notebook was launched from - this is not necessarily the same as the python associated with the kernel (unless the environment you launched from is also the environment associated with the kernel)

# python 3
import subprocess
print(subprocess.getoutput("which python"))
# python 2
import commands
print(commands.getoutput("which python"))

Things I’ve noticed:

r

TODO

# install the R essentials in your current environment
conda install -c r r-essentials

this includes dplyr, shiny, ggplot2, tidyr, caret and nnet

julia

TODO


syntax

magic

# see which magic commands are available in your interpreter
%lsmagic

examples:

command description
%quickref show a quick reference sheet for IPython
%matplotlib inline
%save
%clear
%debug
%time
%timeit
%pdb debug
%prun performance run
%writefile saves contents of a cell to an external file
%pycat shows syntax highlighted contents of an external file
%who list variables of global scope
%store pass variables between notebooks
%load insert code from external script
%run execute python code
%env set environment variables
  • use %% for multiline expressions
  • use ? before the command (or function) to get help

mixing languages

pip install ipython-sql
pip install cython
pip install rpy2
%load_ext sql # pip install ipython-sql
%load_ext cython
%load_ext rpy2.ipython

shell commands

  • use ! before the command
!ls
!cd
!pwd

shortcuts

shortcut action
shift + enter run cell, select below

latex

wrap latex code (e.g. equations) between double dollar signs $$


docker

to run the official Jupyter Notebook image:

docker run --rm -it -p 8888:8888 -v "$(pwd):/notebooks" jupyter/notebook 

other images are available, e.g. 


share

jupyter nbconvert --to html Untitled4.ipynb